package com.neu.servlet.ww;


import com.neu.service.ww.OrderInfoService;
import com.neu.service.ww.OrederService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet(name = "MoneyServlet.do",urlPatterns = "/MoneyServlet.do")
public class MoneyServletdo extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String gids=request.getParameter("gids");
        String a_id=request.getParameter("sele");
        String nums=request.getParameter("nums");
        String price=request.getParameter("price");
        System.out.println(nums+""+price);
        System.out.println(gids);
        String[] gid=gids.split(",");
        List<String> ids=new ArrayList<>();
        for (int i = 0; i < gid.length; i++) {
            if (!"".equals(gid[i])&& gid[i].length()>0){
                ids.add(gid[i]);
            }
        }
        gid=ids.toArray(new String[ids.size()]);
        HttpSession session=request.getSession();
        List<Map<String,Object>> carList= (List<Map<String, Object>>) session.getAttribute("carList");
        List<Map<String,Object>> moneyList=new ArrayList<>();
        for (int i = 0; i < carList.size(); i++) {
            for (int j = 0; j < gid.length; j++) {
                System.out.println(1);
                if (Integer.parseInt(carList.get(i).get("G_ID").toString())==Integer.parseInt(gid[j])){
                    moneyList.add(carList.get(i));
                }
            }
        }
        System.out.println(2);
        Map<String,Object> user= (Map<String, Object>) session.getAttribute("user");
        System.out.println(user.get("U_ID").toString());
        System.out.println(a_id);
        //加入订单表
        OrederService orederService=new OrederService();
        int row=orederService.doInsertOrder(a_id,user.get("U_ID").toString(),nums,price);
        System.out.println(3);
        //加入订单明细表
        System.out.println(moneyList);
        OrderInfoService orderInfoService=new OrderInfoService();
        int row1=orderInfoService.doInsertOrderInfo(moneyList);
        System.out.println(row);
        if (row>0 && row1>0){
            request.getRequestDispatcher("/WEB-INF/views/tiaozhuan.jsp").forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
